import jinja2
import os
import json
from config.cluster_config_new import cluster_client

def read_template(template_path):

    with open(template_path, "r") as fin:
        template_data = fin.read()

    return template_data

def generate_from_template_dict(template_data, map_table):

    generated_file = jinja2.Template(template_data).render(
        map_table
    )

    return generated_file

def write_generated_file(generated_file, file_path):

    with open(file_path, "w+") as fout:
        fout.write(generated_file)


def generate_template():
    print("begining to generate template.sh for machine")
    script_dir = os.path.dirname(os.path.abspath(__file__))
    script_dir = os.path.join(script_dir, "worker_node_add")
    template_file_path = os.path.join(script_dir, "template.sh.template")
    generated_file_path = os.path.join(script_dir, "template.sh")
    template_dict = {}
    # template_dict["KUBE_VERSION"] = os.environ["KUBE_VERSION"]
    # template_dict["DOCKER_CE_VESION"] = os.environ["DOCKER_CE_VESION"]
    # template_dict["K8S_NODE_JOIN_CMD"] = os.environ["K8S_NODE_JOIN_CMD"]
    template_dict["DOCKER_CE_VESION"] =  cluster_client.cluster_config["docker"]["version"]

    template_dict["KUBE_VERSION"] = cluster_client.cluster_config["kubernetes"]["version"]
    template_dict["K8S_NODE_JOIN_CMD"] =  cluster_client.cluster_config["kubernetes"]["node_join_cmd"]
    # template_dict["NV_DRIVER_URL"] = cluster_client.cluster_config["device"]["gpu"]["driver_url"]

    template_file_data = read_template(template_file_path)
    generated_file_data = generate_from_template_dict(template_file_data, template_dict)
    write_generated_file(generated_file_data, generated_file_path)

    print("end to generate template.sh for machine")

def generate_docker_login_template():
    if "harbor" not in cluster_client.cluster_config:
        print("there is no private harbor")
        return

    print("begining to generate docker_login.sh for machine")
    script_dir = os.path.dirname(os.path.abspath(__file__))
    script_dir = os.path.join(script_dir, "worker_node_add")
    template_file_path = os.path.join(script_dir, "docker_login.sh.template")
    generated_file_path = os.path.join(script_dir, "docker_login.sh")

    template_dict = {}
    template_dict["harbor"] =  cluster_client.cluster_config["harbor"]

    template_file_data = read_template(template_file_path)
    generated_file_data = generate_from_template_dict(template_file_data, template_dict)
    write_generated_file(generated_file_data, generated_file_path)

    print("end to generate docker_login.sh for machine")

def generate_daemon_json():
    script_dir = os.path.dirname(os.path.abspath(__file__))
    name = "docker_daemon_json"
    file_path = os.path.join(script_dir, "{}/{}".format("worker_node_add/setClusterCommon/files", name))

    with open(os.path.join(script_dir,file_path), "w+") as fd:
        json.dump(cluster_client.cluster_config["docker"][name], fd)

def generate_nv_daemon_json():
    script_dir = os.path.dirname(os.path.abspath(__file__))
    name = "nv_docker_daemon_json"
    file_path = os.path.join(script_dir, "{}/{}".format("worker_node_add/setClusterCommon/files", name))

    with open(os.path.join(script_dir,file_path), "w+") as fd:
        json.dump(cluster_client.cluster_config["docker"][name], fd)

def generate_hosts():
    script_dir = os.path.dirname(os.path.abspath(__file__))
    name = "hosts"
    file_path = os.path.join(script_dir, "{}/{}".format("worker_node_add/setClusterCommon/files", name))

    with open(os.path.join(script_dir,file_path), "w+") as fd:
        for dns in cluster_client.cluster_config["resolv"]:
            fd.write("{} {}\n".format(dns["ip"], dns["domain"]))


def generate_config():
    generate_template()
    generate_daemon_json()
    generate_nv_daemon_json()
    generate_hosts()
    generate_docker_login_template()

def tar_file():
    import fabric
    conn = fabric.Connection("localhost")
    file_path = os.path.dirname(os.path.abspath(__file__))
    tar_file_name = "worker_node_add.tar.gz"
    local_tmp_path = os.path.join(file_path, tar_file_name)
    conn.local('tar -czf {} -C {} {}'.format(local_tmp_path, file_path, "worker_node_add"))





